package com.example.homeworkmutualevaluation.Dao;

import com.example.homeworkmutualevaluation.DTO.DetailedTask;
import com.example.homeworkmutualevaluation.Entity.Task;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface TaskDao extends JpaRepository<Task, Integer> {
    /* status：
        0:必须完成，当前未完成    	（系统发的批改任务）
        1:可以不完成，当前未完成     （完成系统的批改任务后，学生自己领的作业）
        2:已完成，并且合法    		（刚提交批改的状态）
        3:已完成，并且不合法		（后续系统发现某个分值不合理的状态）
     */
    @Transactional
    @Modifying
    @Query(value = "INSERT INTO task(`evaluator`, `submitter`, `homeworkid`) VALUES (?1, ?2, ?3)", nativeQuery = true)
    int insert(String evaluator, String submitter, int homeworkid);
    void deleteAllByHomeworkid(int homeworkid);
    @Query(value = "SELECT * FROM task WHERE `evaluator`=?1 AND `homeworkid`=?2 AND `status`<=1", nativeQuery = true)
    List<Task> findAllNeedToDo(String evaluator, int homeworkid);
//    @Query(value = "SELECT * FROM task WHERE `evaluator`=?1 AND `homeworkid`=?2 AND (`status`=2 OR `status`=3) ORDER BY `deadline` DESC", nativeQuery = true)
//    List<Task> findAllHasDone(String evaluator, int homeworkid);
    @Query(value = "SELECT new com.example.homeworkmutualevaluation.DTO.DetailedTask(t.id, t.evaluator, t.homeworkid, t.status, e.score, e.comment, e.datetime) " +
            "FROM Task t, Evaluation e WHERE t.id=e.taskid AND t.evaluator=?1 AND t.homeworkid=?2 AND (t.status=2 OR t.status=3)")
    List<DetailedTask> findAllHasDoneWithDetail(String evaluator, int homeworkid);
    Task findById(int id);
    @Query(value = "SELECT COUNT(*) FROM task WHERE `evaluator`=?1 AND `homeworkid`=?2 AND `status`<=1", nativeQuery = true)
    int countNeedToDo(String evaluator, int homeworkid);
}
